#include <interface\font.h>

#include <gl/gl.h>

#include <iostream>

font::font():   ressource(),
                name( DFT_FT_NAME ),
                filename( DFT_FT_FNAME ),
                size( DFT_FT_SIZE ),
                bold( DFT_FT_BOLD ),
                italic( DFT_FT_ITA ),
                stroke( DFT_FT_STRO ),
                under( DFT_FT_UNDER ),
                created( false )
{
    type = RES_TYPE_FONT;
    RSname = "<FONT:\""+name+"\">";
}

                font::~font(){
    if( created ) destroy();
}

void            font::create(){
	HFONT	font;						// Windows Font ID
	HFONT	oldfont;					// Used For Good House Keeping

    HDC     hDC = wglGetCurrentDC();

    AddFontResource( filename.c_str() );

	fontBase = glGenLists(FONT_CHARNUM);
	font = CreateFont(	-size,
                0,
				0,
				0,
				(bold)?FW_BOLD:FW_NORMAL,
				italic,
				under,
				stroke,
				ANSI_CHARSET,
				OUT_TT_PRECIS,
				CLIP_DEFAULT_PRECIS,
				ANTIALIASED_QUALITY,
				FF_DONTCARE|DEFAULT_PITCH,
				name.c_str());
	oldfont = (HFONT)SelectObject(hDC, font);
	wglUseFontBitmaps(hDC, 32, FONT_CHARNUM, fontBase);
	SelectObject(hDC, oldfont);
	DeleteObject(font);
	created = true;
	reset();

	RemoveFontResource( filename.c_str() );

}
void            font::reload(){
    if( created ) destroy();
    create();
}
void            font::destroy(){
    glDeleteLists(fontBase, FONT_CHARNUM);
    created = false;
}

void            font::setName(string nm){
    name = nm;
    RSname = "<FONT:\""+nm+"\">";
}
void            font::setFileName(string fnm){
    filename = fnm;
}
void            font::setSize(unsigned int sz){
    size = sz;
}
void            font::setStroke(bool st){
    stroke = st;
}
void            font::setItalic(bool it){
    italic = it;
}
void            font::setUnderline(bool ud){
    under = ud;
}
void            font::setBold(bool bd){
    bold = bd;
}

string          font::getName(){
    return name;
}
unsigned int    font::getSize(){
    return size;
}
unsigned int    font::getBase(){
    reset();
    return fontBase;
}
bool            font::isStroke(){
    return stroke;
}
bool            font::isItalic(){
    return italic;
}
bool            font::isUnderline(){
    return under;
}
bool            font::isBold(){
    return bold;
}

bool font::update(){
    if( baseVal == RES_ESSENTIAL ) return true;
    if( !created ) return false;
    validity--;
    if( !validity ) return false;
    return true;
}
